package com.rayo.accesscontrollercopy.model.biz;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

import com.litesuits.common.assist.Network;
import com.rayo.accesscontrollercopy.config.GlobalVariables;
import com.rayo.accesscontrollercopy.model.callback.OnGetDbListener;
import com.rayo.accesscontrollercopy.model.db.bean.ReaderData;
import com.rayo.accesscontrollercopy.model.db.bean.SiteData;
import com.rayo.accesscontrollercopy.model.db.bean.SystemData;
import com.rayo.accesscontrollercopy.model.db.dao.ReaderDataDao;
import com.rayo.accesscontrollercopy.model.db.dao.ReaderDataImpl;
import com.rayo.accesscontrollercopy.model.db.dao.SiteDao;
import com.rayo.accesscontrollercopy.model.db.dao.SiteImpl;
import com.rayo.accesscontrollercopy.model.db.dao.SystemDataDao;
import com.rayo.accesscontrollercopy.model.db.dao.SystemDataImpl;

import java.lang.ref.WeakReference;
import java.util.Locale;

/**
 * Created by yanghaojie on 16/7/13.
 */
public class WelcomeBiz implements IWelcomeBiz {
    private WeakReference<Context> reference;
    private SystemDataDao systemDataDao;
    private ReaderDataDao readerDataDao;
    private SiteDao siteDao;
    private Context mContext;
    public WelcomeBiz(Context context) {
        mContext=context;
        reference = new WeakReference<>(context);
        this.systemDataDao = new SystemDataImpl();
        this.readerDataDao = new ReaderDataImpl();
        this.siteDao = new SiteImpl();
    }

    @Override
    public boolean isNetwork() {
        return Network.isConnectedOrConnecting(reference.get());
    }

    @Override
    public void loadDb(OnGetDbListener onGetDbListener) {
        boolean isNew = false;
//        DbHelp.CreateDb(reference.get());
        SystemData systemData = systemDataDao.findFirst();
        if (null == systemData) {
            isNew = true;
            systemData = new SystemData();
            systemDataDao.addSystem(systemData);
        }
        GlobalVariables.getInstance().setmSystemData(systemData);
        if(systemData.getLang()==3){
            Resources resources = mContext.getResources();
            Configuration config = resources.getConfiguration();
            DisplayMetrics dm = resources.getDisplayMetrics();
            config.locale = Locale.CHINESE;
            resources.updateConfiguration(config, dm);
        }
        else {
            Resources resources = mContext.getResources();
            Configuration config = resources.getConfiguration();
            DisplayMetrics dm = resources.getDisplayMetrics();
            config.locale = Locale.ENGLISH;
            resources.updateConfiguration(config, dm);
        }
        ReaderData readerData = readerDataDao.findFirst();
        if (null == readerData) {
            isNew = true;
            readerData = new ReaderData();
            readerDataDao.addReader(readerData);
        }
        SiteData siteData = siteDao.findFirst();
        if (null == siteData || siteData.getLockid() == 0) {
            siteData=new SiteData();
            siteDao.addSite(siteData);
            isNew = true;
        } else
            GlobalVariables.getInstance().setmSiteData(siteData);
        if (isNew)
            onGetDbListener.onSuccess(null);
        else
            onGetDbListener.onSuccess(siteData);
    }
}
